package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
import cn.com.kh.HttpClientProxy
import cn.com.tpUtil.GetXMLValue
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONObject

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 删除临时表，添加主键
 */
public class apiDelTempAddKey implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(apiDelTempAddKey.class);
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");
		log.debug("start apiDelTempAddKey-----------------");
		log.debug("request--:" + request);


		String getMethod = request.getMethod();
		String callJson = "";
		BufferedReader iny = null;
         def coreResult=null;
		log.debug("getMethod:" + getMethod);
		if (getMethod.equals("GET")) {
			callJson = request.getParameter("data");
			log.debug("原始Get请求参数:" + callJson);
		} else {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				callJson += line;
			}
			log.debug("原始Post请求参数:" + callJson);
		}
		try {


			//删除临时表
			List delTableList=eliteBean.chaxun_sql("select TABLE_NAME from user_tables where (TABLE_NAME LIKE 'TEMPLIST#_%'escape '#' or table_name like 'TEMPLATE#_%'escape '#')","dataSource");
			int sizenum=delTableList.size();
			int delnum=0;
			int delsbnum=0;
              for (int i=0;i<delTableList.size();i++){
				  String TableName=delTableList.get(i).get("TABLE_NAME");
				  //开始执行删除表SQL
				int num=eliteBean.update("drop table  "+TableName+"  ","dataSource");
				  if (num>0){
					  log.debug("删除表成功！表名："+TableName);
					  delnum+=1;
				  }else{
					  log.debug("删除表失败！表名："+TableName);
					  delsbnum+=1;
				  }
			  }
			//查询CustList_开头的表,并且无主键的表
			List CustList=eliteBean.chaxun_sql("select a.TABLE_NAME from user_tables  a \n" +
					"          left join user_cons_columns b on a.table_name=b.table_name \n" +
					"          left join user_constraints c on b.constraint_name=c.constraint_name \n" +
					"          where c.constraint_type is null and a.TABLE_NAME LIKE 'CUSTLIST#_%' escape '#'\n" +
					"          and a.table_name!='CUSTLIST_RIGHT'","dataSource");

			int delnum2=0;
			int delsbnum2=0;
             for (int b=0;b<CustList.size();b++){
				 String TableName2=CustList.get(b).get("TABLE_NAME");

				 //生产主键名称  CUSTOMER_GUID+时间戳
				 String CONSTRAINTName="CUSTOMER_GUID"+System.currentTimeMillis().toString();

				 //开始添加主键
				 int num=eliteBean.update("alter table "+TableName2+" ADD CONSTRAINT "+CONSTRAINTName+" PRIMARY KEY (CUSTOMER_GUID)","dataSource")
				 if (num>0){
					 log.debug("添加主键成功！表名："+TableName2+"主键名称："+CONSTRAINTName);
					 delnum2+=1;
				 }else{
					 log.debug("添加主键失败！表名："+TableName2+"主键名称："+CONSTRAINTName);
					 delsbnum2+=1;
				 }
			 }

			coreResult="1.[本次调用查询临时表数量："+sizenum.toString()+"/总计删除成功数量："+delnum.toString()+"/总计删除失败数量"+delsbnum.toString()+"]"
			+"2.[本次调用查询未添加主键CustList_开头表数量："+CustList.size()+"/总计添加主键成功数量："+delnum2.toString()+"/总计添加索引失败数量"+delsbnum2.toString()+"]"



		} catch (Exception e) {
			log.debug("系统调用异常:" + e.getMessage());
			coreResult="系统调用异常:" + e.getMessage();
		}

		return coreResult;
	}
	
}